package com.mvp.ui.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mvp.R;


public class HeadBar extends RelativeLayout {
    private static final int DFAULT = -999999;

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.head_bar, this, true);
        rl_title = findViewById(R.id.rl_title);
        tv_title_left_back = findViewById(R.id.tv_title_left_back);
        tv_title_titletext = findViewById(R.id.tv_title_titletext);
        tv_title_right1 = findViewById(R.id.tv_title_right1);
        iv_title_right1 = findViewById(R.id.iv_title_right1);
    }

    private RelativeLayout rl_title;//标题栏容器(不包含状态栏)
    private TextView tv_title_left_back;//返回按钮
    private TextView tv_title_titletext;//标题文字
    private TextView tv_title_right1;//右边文字
    private ImageView iv_title_right1;//右边图片1

    public HeadBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HeadBar);

        //隐藏返回按钮
        boolean invisibleBack = ta.getBoolean(R.styleable.HeadBar_invisibleBack, false);

        //右边文字控件
        String rightText = ta.getString(R.styleable.HeadBar_rightText);
        int rightTextColor = ta.getColor(R.styleable.HeadBar_rightTextColor, ContextCompat.getColor(context, R.color.white));
        //标题文字
        String titleText = ta.getString(R.styleable.HeadBar_titleText);
        int titleTextColor = ta.getColor(R.styleable.HeadBar_titleTextColor, ContextCompat.getColor(context, R.color.white));
        //右边图片1
        int rightImg1Res = ta.getResourceId(R.styleable.HeadBar_rightImg1Res, DFAULT);
        //本自定义控件的背景
        int backgroundColor = ta.getColor(R.styleable.HeadBar_backgroundColor, ContextCompat.getColor(context, R.color.colorPrimaryDark));


        ta.recycle();

        tv_title_right1.setVisibility(GONE);
        iv_title_right1.setVisibility(GONE);

        if (invisibleBack) {
            tv_title_left_back.setVisibility(INVISIBLE);
            tv_title_left_back.setEnabled(false);
        }

        //设置标题文字
        if (!TextUtils.isEmpty(titleText)) {
            tv_title_titletext.setText(titleText);
            tv_title_titletext.setTextColor(titleTextColor);
        }

        //设置右边文字
        if (!TextUtils.isEmpty(rightText)) {
            tv_title_right1.setVisibility(VISIBLE);
            tv_title_right1.setText(rightText);
            tv_title_right1.setTextColor(rightTextColor);
        }


        //设置右边边图片1
        if (rightImg1Res != DFAULT) {
            iv_title_right1.setVisibility(VISIBLE);
            iv_title_right1.setImageResource(rightImg1Res);
        }


        //设置本自定义控件的背景色
        rl_title.setBackgroundColor(backgroundColor);

    }

    public HeadBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadBar(Context context) {
        super(context);
        init(context);
    }


    public void setTitle(String title) {
        tv_title_titletext.setText(title);
    }

    public void setTitle(@StringRes int title) {
        tv_title_titletext.setText(title);
    }

    public void invisibleBack(int visibility) {
        tv_title_left_back.setVisibility(visibility);
        tv_title_left_back.setEnabled(visibility == View.VISIBLE);
    }

}
